﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentData;
using Common;
using Framework.Data;

namespace Framework.Core
{
    internal class EmployeeDataAccess : BaseDataAccess
    {
        internal User LoadUserInfo(string userName)
        {
            var sql = "SELECT [Employee_Info].[sEmployeeID],[Employee_Info].[sUserName], [Employee_Info].[sEmployeeName], [Employee_Sta].[sDefaultHotelID],[Employee_Info].[sEmail]," +
            "[Employee_Info].[sMobile], [Employee_Sta].[bIsJuniorRec], [Employee_Sta].[bIsJuniorRes], [Employee_Sta].[iRecCnt], [Employee_Sta].[iResCnt]," +
            "[Employee_Sta].[sDefaultPage], [Employee_Info].[sOrgID], [Employee_Info].[sCardNo], [Position_Info].[sPositionName], [Dept_Info].[sDeptName] " +
            "FROM [Employee_Info] LEFT OUTER JOIN [Position_Info] ON [Employee_Info].[sPositionID]=[Position_Info].[sPositionID] LEFT OUTER JOIN [Employee_Sta] " +
            "ON [Employee_Sta].[sEmployeeID]=[Employee_Info].[sUserName] LEFT OUTER JOIN [Dept_Info] ON [Dept_Info].[sDeptID]=[Employee_Info].[sDeptID] WHERE " +
            "[Employee_Info].[sUserName]=@0 AND [Employee_Info].[bIsValid]=1";

            using (var context = DataHelper.GetBaseDbContext())
            {
                var user = context.Sql(sql, userName).QuerySingle<User>(delegate(User userMap, IDataReader reader)
                {
                    userMap.Id = reader.GetString("sEmployeeID");

                    userMap.Name = reader.GetString("sUserName");

                    userMap.EmployeeName = reader.GetString("sEmployeeName");

                    userMap.Email = reader.GetString("sEmail");

                    userMap.Mobile = reader.GetString("sMobile");

                    //user.OrgScope = reader.GetString("sEmployeeName");

                    userMap.EmployeeCardNumber = reader.GetString("sCardNo");



                    userMap.Position = reader.GetString("sEmployeeName");

                    userMap.DefaultHotelId = reader.GetString("sDefaultHotelID");

                    userMap.LoginHotelId = userMap.DefaultHotelId;
                    userMap.IsValid = true;

                    userMap.IsAuthenticated = true;
                    //user.Dept = reader.GetString("sEmployeeName");
                    userMap.Roles = new List<RoleInfo>();

                    //如果没有记载接待次数等信息，则为普通记录
                    //if ((DBNull.Value).Equals(isJuniorRec) || (DBNull.Value).Equals(isJuniorRes) ||
                    //    (DBNull.Value).Equals(recCnt) || (DBNull.Value).Equals(resCnt))
                    //{
                    //    user.Profile = new UserProfile(user.Name);
                    //}

                    //    //如果有记载接待次数等信息，则为接待预定人员特殊记录
                    //else
                    //{
                    //    RsvnClerkProfile profile = new RsvnClerkProfile(user.Name);

                    //    profile.IsJuniorRec = ((bool)isJuniorRec);

                    //    profile.IsJuniorRes = ((bool)isJuniorRes);

                    //    profile.RecCount = ((int)recCnt);

                    //    profile.ResCount = ((int)resCnt);

                    //    user.Profile = profile;
                    //}

                    //user.Profile.LoginOrgId = defaultHotelId;

                    //user.Profile.DefaultHotelId = defaultHotelId;

                    //user.Profile.DefaultPage = defaultPage;

                    //TODO:更多字段
                });
                return user;
            }
        }

        internal User VerifyPassword(string userName, string password)
        {
            var sql = "SELECT Employee_Info.sUserName, Employee_Info.dPasswordExpireDate FROM"
                            + " Employee_Info  "
                            + "WHERE Employee_Info.sUserName=@USER_NAME AND Employee_Info.sPassWord=@PASSWORD AND Employee_Info.bIsValid=1";

            string securityPassword = CipherManager.EncryptPassword(password);

            using (var context = DataHelper.GetBaseDbContext())
            {
                var user = context.Sql(sql).Parameter("USER_NAME", userName).Parameter("PASSWORD", securityPassword).QuerySingle<User>(delegate(User userMap, IDataReader reader)
                    {
                        userMap.Name = reader.GetString("sUserName");
                        userMap.PasswordExpireDate = reader.GetDateTime("dPasswordExpireDate");
                        //TODO:更多字段
                    });

                return user;
            }
        }
    }
}
